package com.itheima.service.impl;

import cn.itheima.health.service.ReportService;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.SetmealMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;


    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public Map getMemberReport() {

        Map map = new HashMap();


        List<String>  yearsandMonthsList = new ArrayList<String>();
        List<Integer>  memberNumbersList = new ArrayList<Integer>();

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);

        for(int i =1;i<=12;i++){
            Date time = calendar.getTime(); //  2019-8

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String format = simpleDateFormat.format(time);//  2019-8

            String beginDate = format+"-1";
            String endDate = format+"-31";

            //  Long  mnum =  mapp.find(xx,xx);

            System.out.println(format);
            yearsandMonthsList.add(format); //   存储过去一年年月

             //  过去每个月的会员数量统计
            int num =  memberMapper.countsMembersByPerMonth(beginDate,endDate);
            memberNumbersList.add(num);
            calendar.add(Calendar.MONTH,1);
        }


        map.put("yearAndMonth",yearsandMonthsList);
        map.put("memberNumbers",memberNumbersList);
        return map;
    }

    @Override
    public List<Map> getSetmealReport() {

        List<Map>  listMap =   orderMapper.countSetMealCountsAndNames();

        return listMap;
    }
}
